import Vue from 'vue'
import home from './home/index.js'
import {
	RouterMount,
	createRouter
} from '../uni-simple-router/uni-simple-router.js'
import {
	loadFontFace
} from '@/utils/loadFontFaceBox.js';
import {
	http
} from '@/utils/http.js'

import {
	getStorage,
	setStorage
} from '@/utils/setStorage.js';
import {
	setTabBar
} from '@/utils/setTabBar.js'
import store from '@/store/index.js'
const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	routerBeforeEach: (to, from, next) => {
		const arr = ['/pages/logins/index', '/pages/home/index', '/pages/logisticsIntroduction/index',
			'/pages/nineGrid/index',
			'/pages/cooperation/index',
			'/pages/cartoon/index', '/pages/productIntroduction/index', '/pages/FAQ/index',
			'/pages/businessCard/index', '/pages/logins/phone', '/pages/logins/registerSubmit',
			'/pages/logins/modifyMM', '/pages/logins/Identification', '/pages/AboutUs/index',
			'/pages/Journalism/index', '/pages/popupPages/index', '/pages/Journalism/JournalismDetails',
			'/pages/me/versionInformation', '/pages/goodsListPicture/index', '/pages/productType/index'
		]
		if (arr.includes(to.path)) {
			if (to.path !== '/pages/home/index') {
				setStorage('loginShow', 1)
			}
			next();
		} else {
			const token = getStorage('token')
			if (!token) {
				let typeBox = ''
				if (getStorage('tokenNum')) {
					typeBox = getStorage('tokenNum').type || 'login'
				} else {
					typeBox = 'login'
				}
				if (typeBox === 'login') {
					setStorage('loginShow', 2)
				} else {
					setStorage('loginShow', 1)
				}

				return next({
					name: 'home',
					NAVTYPE: 'replaceAll',
					success: () => {},
					// params: {
					// 	login: loginShow,
					// }
				})
			} else {
				setStorage('loginShow', 1)
				next();
			}
		}
		// if(to.path==='/pages/maps/index'){
		// 	return next({
		// 		name:'home',
		// 		NAVTYPE:'replaceAll',
		// 		success:()=>{

		// 		},
		// 		params:{
		// 			login: true,
		// 		}
		// 	})
		// }
		// next()
		// 底部tabbar多语言问题
		setTabBar()
		// tabbar选中问题
		store.commit("SET_TABBARTYPE", to.path)
	},
	// routerAfterEach: (to, from) => {
	// 	console.log('-------------------------routerAfterEach-------------------------------------')
	// 	console.log(to)
	// 	console.log(from);
	// 	console.log('-------------------------routerAfterEach结束-------------------------------------')
	// },
	APP: {
		holdTabbar: true, // 默认true  
		animation: {
			animationDuration: 1000
		}
	},
	h5: {
		vueRouterDev: false, // 完全使用vue-router开发 默认 false    
		// paramsToQuery: true,
	},
	debugger: true,
	keepUniOriginNav: false,
	// detectBeforeLock: (...args) => {
	// 	console.log(args)
	// },
	routes: home
});
// 导航前调用
router.beforeEach(async (to, from, next) => {
	// const token = getStorage('token')
	// if (token) {
	// 	const user = JSON.parse(getStorage('userconter'));
	// 	const arr = [{
	// 			url: '/pages/formPages/index',
	// 			type: 'priceInfo'
	// 		}, {
	// 			url: '/pages/maps/index',
	// 			type: 'orderInfo'
	// 		},
	// 		{
	// 			url: '/pages/express/index',
	// 			type: 'onlineOrder'
	// 		},
	// 		{
	// 			url: '/pages/maps/orderTracking',
	// 			type: 'orderTracking'
	// 		},
	// 		{
	// 			url: '/pages/exchangeRate/index',
	// 			type: 'rateConvert'
	// 		},
	// 		{
	// 			url: '/pages/me/modifyPhoneTwo',
	// 			type: 'freightInfo'
	// 		},
	// 		{
	// 			url: '/pages/me/shippingFeeDetail',
	// 			type: 'freightDetail'
	// 		},
	// 		{
	// 			url: '/pages/me/modifyPhone',
	// 			type: 'paymentInfo'
	// 		},
	// 		{
	// 			url: '/pages/me/modifyMail',
	// 			type: 'customerInfo'
	// 		},
	// 		{
	// 			url: '/pages/me/personal',
	// 			type: 'remittanceHistory'
	// 		}
	// 	]
	// 	const params = {
	// 		customerCode: user.customerCode
	// 	}
	// 	let cdStop = '';
	// 	var shipmentImage = setTimeout(() => {
	// 		if (cdStop) {
	// 			cdStop.abort()
	// 		}
	// 	}, 2000)
	// 	const isOks = arr.find(item => item.url == to.path)
	// 	if (isOks) {
	// 		try {
	// 			const {
	// 				data: res
	// 			} = await http(true, 'api/customer/permission', params, 'GET', cd => {
	// 				cdStop = cd;
	// 			});
	// 			clearTimeout(shipmentImage);
	// 			if (res.errno === 0) {
	// 				setStorage('jurisdiction', JSON.stringify(res.data) || null)
	// 				const isGo = arr.find(item => item.url == to.path)
	// 				if (isGo) {
	// 					if (res.data) {
	// 						if (res.data[isGo.type] == 1) {
	// 							next({
	// 								name: 'permissionPage',
	// 								NAVTYPE: 'replaceAll',
	// 								success: () => {},
	// 							});
	// 							next();
	// 						} else {
	// 							next();
	// 						}
	// 					} else {
	// 						next();
	// 					}

	// 				} else {
	// 					next();
	// 				}
	// 			} else {
	// 				next();
	// 			}
	// 		} catch (e) {
	// 			next();
	// 		}
	// 	} else {
	// 		next();
	// 	}
	// } else {
	// 	next();
	// }
	next();
})
// 导航后调用
router.afterEach((to, from) => {
	loadFontFace();
	// console.log(to)
	// console.log(from)
})
Vue.use(router);
export {
	router,
	RouterMount
}
